package com.tedu.springboot2206.controller;

import com.tedu.springboot2206.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File articleDir;
    //创建文章目录
    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()){
            articleDir.mkdirs();
        }
    }
    //发布文章
    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        //获取传递数据
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        //判断输入内容是否规范
        if (title == null || title.trim().isEmpty() || author == null || author.trim().isEmpty()||
                content == null || content.trim().isEmpty()){
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        Article article = new Article(title,author,content);
        System.out.println(article);
        File file = new File(articleDir,title+".obj");
        try (FileOutputStream fos = new FileOutputStream(file);
             ObjectOutputStream oos = new ObjectOutputStream(fos);){
            oos.writeObject(article);
//            response.sendRedirect("/article_success.html");
            response.sendRedirect("/articleList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //展示所有文章
    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response){
        File[] subs = articleDir.listFiles(f->f.getName().endsWith(".obj"));
        List<Article> articleList = new ArrayList<>();
        for (File sub : subs) {
            try (
                    FileInputStream fis = new FileInputStream(sub);
                    ObjectInputStream ois = new ObjectInputStream(fis);){
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList);
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"'1\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>内容</td>");
            pw.println("<td>操作</td>");
            pw.println("</tr>");
            for (Article article : articleList) {
                pw.println("<tr>");
                pw.println("<td>"+article.getTitle()+"</td>");
                pw.println("<td>"+article.getAuthor()+"</td>");
                pw.println("<td><textarea col='30' row='5'>"+article.getContent()+"</textarea></td>");
                pw.println("<td><a href='/changeArticle?title="+article.getTitle()+"&content='windows.document.getElementById('change').value'>修改</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a href='/delArticle?title="+article.getTitle()+"'>删除</a></td>");
                pw.println("</tr>");
            }
            pw.println("<tr>");
            pw.println("<td colspan = \"4\" align='right' ><a href=\"index.html\">回到首页</a></td>");
            pw.println("</tr>");
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/delArticle")
    public void delArticle(HttpServletRequest request,HttpServletResponse response){
        String title = request.getParameter("title");
        File file = new File(articleDir,title+".obj");
        if (file.exists()){
            file.delete();
        }
        try {
            response.sendRedirect("/articleList");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("changeArticle")
    public void changeArticle(HttpServletRequest request,HttpServletResponse response){
        System.out.println("======");
        System.out.println(request.getParameter("content"));
    }
}
